package com.yunchao.app.presentation.splash

import android.annotation.SuppressLint
import android.content.Intent
import android.view.KeyEvent
import com.xuexiang.xui.utils.KeyboardUtils
import com.xuexiang.xui.widget.activity.BaseSplashActivity
import com.yunchao.app.presentation.configure.ConfigureActivity.Companion.openConfigure
import com.yunchao.app.presentation.main.NavHostActivity
import com.yunchao.feature.base.HTTP_DOMAIN_INPUT_URL
import com.yunchao.feature.base.data.CacheManager
import com.yunchao.feature.base.domain.constants.SERVER_IP_KEY
import com.yunchao.feature.base.domain.constants.SERVER_PORT_KEY
import me.jessyan.retrofiturlmanager.RetrofitUrlManager
import timber.log.Timber
import kotlin.apply
import kotlin.jvm.java

@SuppressLint("CustomSplashScreen")
class SplashActivity : BaseSplashActivity() {


    override fun onCreateActivity() {
        startSplash(true)
    }

    override fun onSplashFinished() {
        val serverIP = CacheManager.getString(SERVER_IP_KEY)
        if (serverIP?.isNotBlank() == true) {
            RetrofitUrlManager.getInstance().putDomain(
                HTTP_DOMAIN_INPUT_URL,
                "http://$serverIP:${CacheManager.getString(SERVER_PORT_KEY)}"
            ).apply {
                startActivity(Intent(this@SplashActivity, NavHostActivity::class.java)).apply {
                    finish()
                }
            }
        } else {
            openConfigure(this@SplashActivity).apply { finish() }
        }
    }


    /**
     * 菜单、返回键响应
     */
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return KeyboardUtils.onDisableBackKeyDown(keyCode) && super.onKeyDown(keyCode, event)
    }
}